﻿using log4net;
using StoreProject.BLL.SettingBLL;
using StoreProject.DAL.SettingDAL;
using StoreProject.Entity.SettingEntity;
using StoreProject.Enum;
using StoreProject.Forms.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StoreProject.Forms.SettingForms
{
    public partial class WorkingProcedureRemarkForm : ConfigQueryForm
    {
        ILog _log = LogManager.GetLogger("WorkingProcedureRemarkForm");
        WorkingProcedureRemarkBLL workingProcedureRemarkBLL = new WorkingProcedureRemarkBLL();
        public WorkingProcedureRemarkForm()
        {
            InitializeComponent();
        }

        private void WorkingProcedureRemarkForm_Load(object sender, EventArgs e)
        {
            uiDataGridView1.AutoGenerateColumns = false;
            List<WorkingProcedureRemarkEntity> data = new List<WorkingProcedureRemarkEntity>();
            data = workingProcedureRemarkBLL.GetList().ToList();
            this.Query();
        }
        #region 顶部菜单重写继承方法

        public override void Add()
        {
            WorkingProcedureRemarkSettingForm workingProcedureRemarkSettingForm = new WorkingProcedureRemarkSettingForm(FormState.ADD);
            workingProcedureRemarkSettingForm.TopMost = true;
            workingProcedureRemarkSettingForm.ShowDialog();
            workingProcedureRemarkSettingForm.BringToFront();
            this.Query();
        }

        public override void Cancel()
        {

        }

        public override void Exit()
        {
            this.Close();
        }
        public override void OnUpdate()
        {
            WorkingProcedureRemarkEntity workingProcedureRemarkEntity = this._currentObj as WorkingProcedureRemarkEntity;
            if (CanOperator() && !string.IsNullOrEmpty(workingProcedureRemarkEntity.Id))
            {
                WorkingProcedureRemarkSettingForm workingProcedureRemarkSettingForm = new WorkingProcedureRemarkSettingForm(FormState.UPDATE, workingProcedureRemarkEntity);
                workingProcedureRemarkSettingForm.TopMost = true;
                workingProcedureRemarkSettingForm.ShowDialog();
                workingProcedureRemarkSettingForm.BringToFront();
                this.Query();
            }
            else
            {
                MessageBox.Show("请选中要更新的行数据。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        public override void OnDelete()
        {
            WorkingProcedureRemarkEntity workingProcedureRemarkEntity = this._currentObj as WorkingProcedureRemarkEntity;
            if (CanOperator() && !string.IsNullOrEmpty(workingProcedureRemarkEntity.Id))
            {
                bool res = workingProcedureRemarkBLL.Delete(workingProcedureRemarkEntity);
                if (res)
                {
                    MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Query();
                }
                else
                {
                    MessageBox.Show("删除失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的行数据！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        public override void Query()
        {
            try
            {
                List<WorkingProcedureRemarkEntity> data = new List<WorkingProcedureRemarkEntity>();
                data = workingProcedureRemarkBLL.GetList().ToList();
                if (data != null && data.Count > 0)
                {
                    this.uiDataGridView1.DataSource = data;
                }
                else
                {
                    uiDataGridView1.DataSource = null;
                    //cmbCustomer.DataSource = data;
                }

            }
            catch (Exception ex)
            {
                _log.Error("Query", ex);
            }
        }
        #endregion
        private bool CanOperator()
        {
            if (_currentObj == null)
                return false;
            return true;
        }

        #region DataGridView相关方法

        private void uiDataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    uiDataGridView1.ClearSelection();
                    uiDataGridView1.Rows[e.RowIndex].Selected = true;
                    uiDataGridView1.CurrentCell = uiDataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

        private void uiDataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            if (dgv.CurrentRow != null)
            {
                this._currentObj = dgv.CurrentRow.DataBoundItem;
            }
            else
            {
                this._currentObj = null;
            }
        }

        #endregion

        #region 右键菜单相关方法

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.OnDelete();
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Add();
        }
        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.OnUpdate();
        }

        #endregion
    }
}
